/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * UsermgrServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Common;
import com.sundy.core.Configuration;
import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.daosupport.DescriptTblDAO;
import com.sundy.daosupport.RightTblDAO;
import com.sundy.daosupport.SubjectTblDAO;
import com.sundy.daosupport.TeacherTblDAO;
import com.sundy.service.DescriptTbl;
import com.sundy.service.RightTbl;
import com.sundy.service.TeacherTbl;
import com.sundy.tools.WebUtility;

public class UsermgrServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6096161571337300224L;

	public UsermgrServlet() {
	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(context, request, response);
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		String id = request.getParameter("id");
		if (action == null || action.equals("")) {
			Utility.showError(request, response, "参数错误");
			return;
		} else if (action.equals("delete")) {
			TeacherTblDAO dao = TeacherTblDAO
					.getFromApplicationContext(Common.applicationContext);
			TeacherTbl item = dao.findById(Integer.valueOf(id));
			dao.delete(item);
			dao.getHibernateTemplate().flush();
		} else if (action.equals("update")) {
			TeacherTblDAO dao = TeacherTblDAO
					.getFromApplicationContext(Common.applicationContext);
			TeacherTbl item = dao.findById(Integer.valueOf(id));
			String right = request.getParameter("right");
			if (right == null || right.equals("")) {
				Utility.showError(request, response, "参数错误");
				return;
			}
			item.setAdmin(Integer.valueOf(right));
			dao.merge(item);
			dao.getHibernateTemplate().flush();
		} else if (action.equals("updatedesc")) {
			String descript = request.getParameter("descript");
			DescriptTblDAO dao = DescriptTblDAO
					.getFromApplicationContext(Common.applicationContext);
			DescriptTbl item = dao.findById(Integer.valueOf(id));
			item.setFinished(Integer.valueOf(descript));
			dao.merge(item);
			dao.getHibernateTemplate().flush();
		} else if (action.equals("descdel")) {
			DescriptTblDAO dao = DescriptTblDAO
					.getFromApplicationContext(Common.applicationContext);
			DescriptTbl item = dao.findById(Integer.valueOf(id));
			dao.delete(item);
			dao.getHibernateTemplate().flush();
		} else if (action.equals("passmodify")) {
			String password = request.getParameter("pass1");
			if (password == null || password.trim().equals("")) {
				Utility.showInfor(request, response, "密码不能设置为空！");
				return;
			}
			TeacherTblDAO dao = TeacherTblDAO
					.getFromApplicationContext(Common.applicationContext);
			TeacherTbl user = context.getCurrentUser();
			user.setPassword(password);
			dao.merge(user);
			dao.getHibernateTemplate().flush();
		} else if (action.equals("deletefile")) {
			String file = request.getParameter("file");
			if (file == null || file.trim().equals("")) {
				Utility.showInfor(request, response, "参数错误！");
				return;
			}
			String dir = Configuration.getProperty("/config/export-dir");
			File exportFile = new File(dir + File.separator + file);
			exportFile.delete();
		} else if (action.equals("scoremax")) {
			String idStr = request.getParameter("id");
			String max = request.getParameter("max");
			if (idStr == null || idStr.trim().equals("")) {
				Utility.showInfor(request, response, "参数错误！");
				return;
			}
			if (max == null || max.trim().equals("")) {
				Utility.showInfor(request, response, "参数错误！");
				return;
			}
			WebUtility.setSubjectMax(idStr, max);
		} else if (action.equals("subdel")) {
			String idStr = request.getParameter("id");
			if (idStr == null || idStr.trim().equals("")) {
				Utility.showInfor(request, response, "参数错误！");
				return;
			}
			SubjectTblDAO subdao = SubjectTblDAO
					.getFromApplicationContext(Common.applicationContext);
			subdao.delete(Integer.valueOf(idStr));
			subdao.getHibernateTemplate().flush();
		} else if (action.equals("mapping")) {
			// subject=31&subject=32&subject=33&subject=34&subject=35&subject=37&action=mapping&id=7
			String idStr = request.getParameter("id");
			if (idStr == null || idStr.trim().equals("")) {
				Utility.showInfor(request, response, "参数错误！");
				return;
			}
			String[] subs = request.getParameterValues("subject");
			if (subs == null) {
				Utility.showInfor(request, response, "参数错误！哥哥，好歹选一门吧~_~");
				return;
			}
			RightTblDAO dao = RightTblDAO
					.getFromApplicationContext(Common.applicationContext);
			List<RightTbl> list = dao.findByTeacherId(Integer.valueOf(idStr));
			for (RightTbl r : list)
				dao.delete(r);
			dao.getHibernateTemplate().flush();
			for (String subid : subs) {
				RightTbl ri = new RightTbl(Integer.valueOf(idStr), Integer
						.valueOf(subid));
				dao.save(ri);
			}
			dao.getHibernateTemplate().flush();
		} else {
			Utility.showInfor(request, response, "嘘唏！系统没有找到该动作列表！");
			return;
		}
		response.sendRedirect(request.getContextPath());
	}
}
